﻿using AIStudio.Core;
using AIStudio.Wpf.BasePage.Events;
using AIStudio.Wpf.Business;
using AIStudio.Wpf.Client.Views;
using AIStudio.Wpf.Service.IWebSocketClient;
using Prism.Events;
using Prism.Regions;
using Squirrel;
using System.Windows;
using Util.Controls;

namespace AIStudio.Wpf.Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : WindowBase
    {
        IRegionManager _regionManager { get; }
        private IEventAggregator _eventAggregator { get; }
        IWSocketClient _wSocketClient { get; }
        ILocalSetting _localSetting { get; }
        ILogger _logger { get; }

        public MainWindow(IRegionManager regionManager, IEventAggregator eventAggregator, IWSocketClient wSocketClient, ILocalSetting localSetting, ILogger logger)
        {
            HomePage.ViewModels.SystemSetViewModel.InitSetting();

            InitializeComponent();

            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            _wSocketClient = wSocketClient;
            _localSetting = localSetting;
            _logger = logger;

            ShowLogin();                       
        }

        private void LoginSentEventReceived(bool loginState)
        {
            if (loginState == false)
            {
                //直接重启应用，比较暴力，如果应用程序中有大量缓存数据要保存，此处优化
                System.Windows.Forms.Application.Restart();
                Application.Current.Shutdown();
            }
        }

        private void ShowLogin()
        {
            this.Hide();

            UpdateWindow update = new UpdateWindow(_localSetting, _logger);
            if (update.ShowDialog() == false)
            {
                Application.Current.Shutdown();
                return;
            }

            LoginWindow login = new LoginWindow(_eventAggregator);
            if (login.ShowDialog() == false)
            {
                Application.Current.Shutdown();
                return;
            }
            this.Show();
            this.Closing += MainWindow_Closing;
            _eventAggregator.GetEvent<LoginSentEvent>().Subscribe(LoginSentEventReceived);
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult result = MessageBoxHelper.ShowSure("确定要退出系统?", this);
            if (result != MessageBoxResult.OK)
            {
                e.Cancel = true;
            }
            else
            {
                _wSocketClient.Dispose();
                System.Windows.Application.Current.Shutdown();
                //System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
        }

        int index = 0;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var flyout = this.Flyouts.Items[index] as Flyout;
            if (flyout == null)
            {
                return;
            }
            index = 1;

            flyout.IsOpen = !flyout.IsOpen;
        }
    }
}
